package com.jshx.sz.gdjt.control;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jshx.sz.gdjt.R;

/**
 * 页面标题头
 * @author xiangchangan
 * @time 2013-08-26
 */
public class ToggleButtonView extends RelativeLayout {
	
	//
	public static enum ToggleButtonStatu{
		STATU_LEFT ,
		STATU_RIGHT ,
		STATU_NONE
	}

	private final static String TAG = "ToggleButtonView";
	
	/**
	 * 左边按钮
	 */
	private TextView left;
	
	/**
	 * 右边按钮
	 */
	private TextView right;
	
	/**
	 * 左边按钮监听器
	 */
	private OnClickListener leftClickListener;
	
	/**
	 * 右边按钮监听器
	 */
	private OnClickListener rightClickListener;
	
	/**
	 * 上下文对象
	 */
	private Context context;
	
	//
	private Enum toggleButtonStatu;
	
	public ToggleButtonView(Context context) {
		super(context);
		this.context = context;
	}
	
	public ToggleButtonView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() 
	{
		super.onFinishInflate();
		left = (TextView)findViewById(R.id.toggle_left);
		right = (TextView)findViewById(R.id.toggle_right);
		left.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				toggleButtonStatu = ToggleButtonStatu.STATU_LEFT;
				left.setBackgroundResource(R.drawable.left_pressed);
				right.setBackgroundResource(R.drawable.right_normal);
				left.setTextColor(context.getResources().getColor(R.color.white));
				right.setTextColor(context.getResources().getColor(R.color.gray));
				if(leftClickListener != null)
				{
					leftClickListener.onClick(v);
				}
			}
		});
		right.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				toggleButtonStatu = ToggleButtonStatu.STATU_RIGHT;
				left.setBackgroundResource(R.drawable.left_normal);
				right.setBackgroundResource(R.drawable.right_pressed);
				left.setTextColor(context.getResources().getColor(R.color.gray));
				right.setTextColor(context.getResources().getColor(R.color.white));
				if(rightClickListener != null)
				{
					rightClickListener.onClick(v);
				}
			}
		});
		toggleButtonStatu = ToggleButtonStatu.STATU_LEFT;
	}
	
	/**
	 * 左边按钮点击监听
	 */
	public void setLeftButtonClickListener(OnClickListener l)
	{
		if(l != null)
		{
			leftClickListener = l;
		}
	}
	
	/**
	 * 右边按钮点击监听
	 */
	public void setRightButtonClickListener(OnClickListener l)
	{
		if(l != null)
		{
			rightClickListener = l;
		}
	}

	public Enum getToggleButtonStatu() {
		return toggleButtonStatu;
	}

	public void setToggleButtonStatu(Enum toggleButtonStatu) 
	{
		this.toggleButtonStatu = toggleButtonStatu;
		if(toggleButtonStatu == ToggleButtonStatu.STATU_LEFT)
		{
			left.setBackgroundResource(R.drawable.left_pressed);
			right.setBackgroundResource(R.drawable.right_normal);
			left.setTextColor(context.getResources().getColor(R.color.white));
			right.setTextColor(context.getResources().getColor(R.color.gray));
		}
		else if(toggleButtonStatu == ToggleButtonStatu.STATU_RIGHT)
		{
			left.setBackgroundResource(R.drawable.left_normal);
			right.setBackgroundResource(R.drawable.right_pressed);
			left.setTextColor(context.getResources().getColor(R.color.gray));
			right.setTextColor(context.getResources().getColor(R.color.white));
		}
		else 
		{
			left.setBackgroundResource(R.drawable.left_normal);
			right.setBackgroundResource(R.drawable.left_normal);
			left.setTextColor(context.getResources().getColor(R.color.gray));
			right.setTextColor(context.getResources().getColor(R.color.gray));
		}
	}
}
